#include <events/common_log.h>
#include "ssicloud_trip_dashboard.h"

using namespace ssicloud;
TripDashboard::TripDashboard()
    : ItemObject<TripNode>(LQ_BASE_URL "/trip/dashboard"
                           , OBJECT_NAME_TripDashboard
                           , NetUrl::NET_HTTP_METHOD_POST)
{    
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool TripDashboard::requestAll(const char *start, const char *end)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                 "\"start\": \"%s\","
                                 "\"end\": \"%s\""
                                 "}", VIN_STRING, start, end);
    mUrl.appendBody(m_body);
    return obtain();
}
// {"msg":"成功","data":{"avgTrips":0.0,"trips":0,"days":31,"hours":407.98,"kmFuels":24.39,"fuels":6607.5,"miles":27093.2,"avgSpeed":66.41,"times":407.98,"avgFuels":24.39,"svgSpeed":66.41,"oilFuel":6607.5,"electricFuel":0.0,"gasFuel":0.0},"code":1
int TripDashboard::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, fuels, mItem.fuels);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, oilFuel, mItem.oilFuel);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, gasFuel, mItem.gasFuel);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, miles, mItem.miles);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, times, mItem.times);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, svgSpeed, mItem.svgSpeed);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, kmFuels, mItem.kmFuels);

        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgTrips, mItem.avgTrips);
        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, trips, mItem.trips);
        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, days, mItem.days);
        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, hours, mItem.hours);
        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgSpeed, mItem.avgSpeed);
        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgFuels, mItem.avgFuels);
        //JSON_VALUETRING_SWAP_BYTESTRING(dataJson, electricFuel, mItem.electricFuel);

        // profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void TripDashboard::profile()
{
    GEN_Printf(LOG_INFO, "---------数据总览-------");
    GEN_Printf(LOG_INFO, "能耗: %s", mItem.fuels.c_str());
    GEN_Printf(LOG_INFO, "油耗: %s", mItem.oilFuel.c_str());
    GEN_Printf(LOG_INFO, "气耗: %s", mItem.gasFuel.c_str());
    GEN_Printf(LOG_INFO, "里程: %s", mItem.miles.c_str());
    GEN_Printf(LOG_INFO, "运行时长: %s", mItem.times.c_str());
    GEN_Printf(LOG_INFO, "平均速度: %s", mItem.svgSpeed.c_str());
    GEN_Printf(LOG_INFO, "平均能耗: %s", mItem.kmFuels.c_str());
    GEN_Printf(LOG_INFO, "---------完成-------");
}
